package tool;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;

import entity.Suit;

public class ReadResources {
	private Map<Suit, BufferedImage[]> pokers;	//通过花色获取扑克牌BufferedImage
	private Suit suitOrder[];//花色顺序
	private int horStart;//水平起始
	private int verStart;//竖直起始
	private int horSplit;//水平分割
	private int verSplit;//竖直分割
	private int cardWidth;//扑克宽度
	private int cardHeight;//扑克高度
	private BufferedImage pokerBackImage;//扑克背景图片
	private BufferedImage tableBackImage;//牌桌背景图
	private Map<String,String> pokerBackMap;//名称--扑克背景映射
	private Map<String,String> tableBackMap;//名称--牌桌背景映射
	private ReadConfig readConfig;//配置读取器
	public ReadResources(ReadConfig readConfig) {
		pokers=new HashMap<>();//新建一个HashMap
		for(Suit suit:Suit.values()){//为每一个花色添加新的数组
			pokers.put(suit, new BufferedImage[13]);
		}
		/*以下的参数配置以后可以通过读取配置文件来进行获取*/
		suitOrder=new Suit[4];
		suitOrder[0]=Suit.Spade;
		suitOrder[1]=Suit.Heart;
		suitOrder[2]=Suit.Club;
		suitOrder[3]=Suit.Diamond;
		horStart=43;
		verStart=40;
		horSplit=37;
		verSplit=36;
		cardWidth=225;
		cardHeight=345;
		this.readConfig=readConfig;
		
		BufferedImage parent=getPic("timg.jpeg");//读取母版图片
		pokerBackMap=new HashMap<>();
		tableBackMap=new HashMap<>();
		initResource();
		readPokerBack(readConfig.getDefaultPokerBack());
		readTableBack(readConfig.getDefaultTableBack());
		//backImage=getPic("Quinn.jpeg");//读取背景图片
		splitParentImage(parent);//分割母版
		//System.out.println(parent.getWidth()+" "+parent.getHeight());
	}
	/**初始化资源信息
	 * 
	 */
	public void initResource(){
		pokerBackMap=readConfig.readPokerBack();
		tableBackMap=readConfig.readTableBack();
	}
	/**获取资源路径
	 * 
	 * @return
	 */
	private String getResourcesPath(){
		String res="file:";
		try {
			res=URLDecoder.decode(this.getClass().getResource("/").toString(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res.substring(5,res.length());
	}
	/**读取一个文件
	 * 
	 * @param filename 文件名
	 * @return
	 */
	public BufferedImage getPic(String filename){
		BufferedImage image = null;
        try {
            image = ImageIO.read(new File(getResourcesPath()+filename));
        } catch (IOException e) {
        	e.printStackTrace();
        }
        return image;
	}
    /**
     * 将一张图片按照规则切分图片
     * @param image
     * @return
     */
    public void splitParentImage(BufferedImage image) {
    	int verCur=verStart,horCur=horStart;//设置当前的分割位置
    	for(int i=0;i<4;i++){//读取行--花色
    		for(int j=0;j<13;j++){//读取列--数字
    			pokers.get(suitOrder[i])[j]=image.getSubimage(horCur, verCur, cardWidth, cardHeight);//读取一张扑克
    			horCur+=horSplit+cardWidth;//偏移一个水平间隔和一个扑克宽度
    			if(j>8)
    				horCur+=14;
    		}
    		horCur=horStart;//偏移到水平位置
    		verCur+=verSplit+cardHeight;//偏移一个竖直间隔和一个扑克高度
    	}
    	/*这张图的黑桃456顺序是645，因此在这里进行一下修改，以后可以选择更好的母版*/
    	BufferedImage tempImage=pokers.get(suitOrder[0])[3];//拿到6
    	pokers.get(suitOrder[0])[3]=pokers.get(suitOrder[0])[4];//把4赋值过去
    	pokers.get(suitOrder[0])[4]=pokers.get(suitOrder[0])[5];//把5赋值过去
    	pokers.get(suitOrder[0])[5]=tempImage;//把6赋值过去
    }
    /**根据花色和数字获取一张图片
     * 
     * @param suit 花色
     * @param number 数字
     * @return 图片
     */
    public BufferedImage getPoker(Suit suit,int number){
    	return pokers.get(suit)[number-1];
    }
    /**获取扑克背景图片
     * 
     * @return
     */
    public BufferedImage getPokerBack(){
    	return pokerBackImage;
    }
    /**根据名称重置扑克背景
     * 
     * @param name
     */
    public void readPokerBack(String name){
    	if(!pokerBackMap.containsKey(name)){
    		return ;
    	}
    	pokerBackImage=getPic("poker/"+pokerBackMap.get(name));
    }
    /**获取牌桌背景图片
     * 
     * @return
     */
    public BufferedImage getTabelBack(){
    	return tableBackImage;
    }
    /**根据名称重置牌桌背景
     * 
     * @param name
     */
    public void readTableBack(String name){
    	if(!tableBackMap.containsKey(name)){
    		return ;
    	}
    	tableBackImage=getPic("table/"+tableBackMap.get(name));
    }
    /**返回当前可选择的牌背景
     * 
     * @return
     */
	public Set<String> getPokerBackChoice(){
    	return pokerBackMap.keySet();
    }
	/**返回当前可选择的牌桌背景
     * 
     * @return
     */
	public Set<String> getTableBackChoice(){
    	return tableBackMap.keySet();
    }
}
